/**
 *
 */
package com.imonsoft.common.util;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * 
 * @author LiQianHui
 * @date 2013-04-17
 * @version 2013
 */
public abstract class DateUtil {

	private final static Log logger = LogFactory.getLog(DateUtil.class);

	public static String DATE_FORMAT = "yyyy-MM-dd";
	
	public static String DATE_FORMAT_YEAR = "yyyy";

	public static String DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";

	/**
	 * 字符串转换成日期，如果需转换的字符串为null，则返回为null
	 * 
	 * @param value
	 *            String 需转换的字符串 格式为yyyy-MM-dd
	 * @return Date 日期
	 */
	public static Date string2date(String value) {
		if (value == null) {
			return null;
		}
		SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
		try {
			return sdf.parse(value);
		} catch (ParseException e) {
			logger.error(e);
			throw new RuntimeException(e);
		}
	}

	/**
	 * 
	 * @param dateStr
	 *            yyyy-mm-dd hh:mm:ss.fffffffff
	 * @return
	 */
	public static Timestamp string2timestamp(String dateStr) {
		if (dateStr == null) {
			return null;
		}
		String[] ds = dateStr.split("-");
		String[] dh = dateStr.split(" ");
		if (ds.length == 3) {
			if (dh.length == 2) {
				return Timestamp.valueOf(dateStr);
			} else {
				return Timestamp.valueOf(dateStr + " 00:00:00");
			}
		}
		return null;
	}

	/**
	 * 字符串转换成日期，如果需转换的字符串为null，则返回为null
	 * 
	 * @param value
	 *            String 需转换的字符串
	 * @param format
	 *            String 转换成字符型的日期格式
	 * @return Date 日期
	 */
	public static Date string2date(String value, String format) {
		if (value == null) {
			return null;
		}
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		try {
			return sdf.parse(value);
		} catch (ParseException e) {
			logger.error(e);
			throw new RuntimeException(e);
		}
	}

	/**
	 * 日期转换成字符串，如果需转换的日期为null，则返回为null
	 * 
	 * @param date
	 *            Date 需转换的日期 格式为yyyy-MM-dd
	 * @return String 日期字符串
	 */
	public static String date2string(Date date) {
		if (date == null)
			return null;
		SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
		return sdf.format(date);
	}

	/**
	 * 日期转换成字符串，如果需转换的日期为null，则返回为null
	 * 
	 * @param date
	 *            Date 需转换的日期
	 * @param format
	 *            String 转换成字符型的日期格式
	 * @return String 日期字符串
	 */
	public static String date2string(Date date, String format) {
		if (date == null)
			return null;
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.format(date);
	}

	/**
	 * 
	 * @param format
	 *            yyyy-MM-dd HH:mm:ss:S 年月日时分秒毫杪
	 * @return
	 */
	public static String getNowDate(String format) {
		String dateTime = "";
		try {
			Date now = new Date();
			SimpleDateFormat sdf = new SimpleDateFormat(format);
			dateTime = sdf.format(now);
		} catch (Exception e) {
			logger.error(e);
		}
		return dateTime;
	}

	/**
	 * 
	 * @return 以 yyyy-mm-dd 形式返回当前日期
	 */
	public static String getNowDate() {
		return getNowDate(DATE_FORMAT);
	}

	/**
	 * 
	 * @return 以 yyyy-MM-dd HH:mm:ss 形式返回当前日期时间
	 */
	public static String getNowDateTime() {
		return getNowDate(DATE_TIME_FORMAT);
	}

	/**
	 * 
	 * @param date
	 * @param date2
	 * @return date2-date的天数
	 */
	public static long getDaysInterval(Date date, Date date2) {
		return (date2.getTime() - date.getTime()) / 86400000;
	}
	
	/**
	 * 相差date的天数
	 * @param date
	 * @param days
	 * @return
	 */
	public static String getDate(Date date,int days){
		Date dateTemp = new Date();
		dateTemp.setTime(date.getTime() + 86400000L * days);
		return date2string(dateTemp);
	}

	/**
	 * @param days
	 * @return 和当前相差days天的日期
	 */
	public static String getDate(int days) {
		Date date = new Date();
		return getDate(date,days);
	}

	/**
	 * 
	 * @param date
	 * @return date对应的年
	 */
	public static int getYear(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar.get(Calendar.YEAR);
	}

	public static int getNowYear() {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(new Date());
		return calendar.get(Calendar.YEAR);
	}

	/**
	 * 
	 * @param date
	 * @return date对应的月
	 */
	public static int getMonth(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar.get(Calendar.MONTH) + 1;
	}

	public static int getNowMonth() {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(new Date());
		return calendar.get(Calendar.MONTH) + 1;
	}

	public static int getDayOfWeek(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar.get(Calendar.DAY_OF_WEEK);
	}

	public static int getNowDayOfWeek() {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(new Date());
		return calendar.get(Calendar.DAY_OF_WEEK);
	}

	/**
	 * 
	 * @param date
	 * @return 对应的天
	 */
	public static int getDayOfMonth(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar.get(Calendar.DAY_OF_MONTH);
	}

	public static int getNowDayOfMonth() {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(new Date());
		return calendar.get(Calendar.DAY_OF_MONTH);
	}

	public static Timestamp getNowTimestamp() {
		Timestamp timestamp = new Timestamp(System.currentTimeMillis());
		return timestamp;
	}

	public static String getNowYearMonthDayWeek() {
		return DateUtil.getNowDate("yyyy年M月d日 H:m:s") + " 星期"
				+ "日一二三四五六".charAt(getNowDayOfWeek() - 1);
	}
    /**
     * 获取某年份的第一天凌晨
     * @param year
     * @return
     */
    public static Date getYearStart(int year){
        Calendar c=Calendar.getInstance();
        c.set(Calendar.YEAR,year);
        c.set(Calendar.MONTH,Calendar.JANUARY);
        c.set(Calendar.DAY_OF_MONTH,1);
        c.set(Calendar.HOUR_OF_DAY,0);
        c.set(Calendar.MINUTE,0);
        c.set(Calendar.SECOND,0);
        return c.getTime();
    }
    /**
     * 获取某年份下，下个月份的第一天凌晨
     * @param year
     * @param month
     * @return
     */
    public static Date getYearMonthEnd(int year,int month){
        Calendar c=Calendar.getInstance();
        c.set(Calendar.YEAR,year);
        c.set(Calendar.MONTH,month);
        c.set(Calendar.DAY_OF_MONTH,1);
        c.set(Calendar.HOUR_OF_DAY,0);
        c.set(Calendar.MINUTE,0);
        c.set(Calendar.SECOND,0);
        return c.getTime();
    }
    /**
     * 获取某年份下，某月份的第一天凌晨
     * @param year
     * @param month
     * @return
     */
    public static Date getYearMonthStart(int year,int month){
        Calendar c=Calendar.getInstance();
        c.set(Calendar.YEAR,year);
        c.set(Calendar.MONTH,month-1);
        c.set(Calendar.DAY_OF_MONTH,1);
        c.set(Calendar.HOUR_OF_DAY,0);
        c.set(Calendar.MINUTE,0);
        c.set(Calendar.SECOND,0);
        return c.getTime();
    }
	public static void main(String[] args) {
		System.out.println(new Date());
		System.out.println(getNowDate());
		System.out.println(getNowDateTime());
		Date date = string2date("2007-01-06");
		Date date2 = string2date("2007-01-08");
		System.out.println(getDaysInterval(date, date2));
		System.out.println(getYear(date));
		System.out.println(getMonth(date));
		System.out.println(getDayOfMonth(date));
		System.out.println(string2date("2007-01-01"));
		System.out.println(date2string(string2date("2007-01-01 12:13:14", DATE_TIME_FORMAT),
				DATE_TIME_FORMAT));
		System.out.println(date2string(string2date("2007-01-01 12:13:14")));
		System.out.println(DateUtil.getNowDate("yyMMddHHmmss"));
		System.out.println(getDate(-21));
		System.out.println((86400000L * (-300)));
		System.out.println(getNowYear());
		System.out.println("getNowTimestamp:" + getNowTimestamp());
		System.out.println(getNowDayOfWeek());
		System.out.println("星期" + "日一二三四五六".charAt(getNowDayOfWeek() - 1));
		System.out.println(getDayOfWeek(string2date("2009-06-27")));
		System.out.println(getDayOfWeek(string2date("2009-06-28")));
		System.out.println(string2timestamp("2007-01-01 12:13:14.999999999"));
	}
}
